package com.bbs.toutiao.service.client.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.bbs.toutiao.aspect.SysLog;
import com.bbs.toutiao.exception.BaseException;
import com.bbs.toutiao.exception.ExceptionEnum;
import com.bbs.toutiao.mapper.clinet.ClientArticleEditMapper;
import com.bbs.toutiao.service.client.ArticleEditService;
import com.bbs.toutiao.vo.article.ArticlePageRequestVo;
import com.bbs.toutiao.vo.article.ArticleVo;
import com.bbs.toutiao.vo.comm.PageVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


/**
 * 描述: 文章操作service层
 * 作者： zhangBin
 * 时间： 2023/10/8 22:31
 * 版本： 1.0
 */

@Slf4j
@Service
public class ArticleEditServiceImpl implements ArticleEditService {

    @Resource
    ClientArticleEditMapper editMapper;




    /**
     * 新增文章
     *
     * @param article
     * @return int 1新增成功，0新增失败
     */
    @SysLog("用户发布了文章")
    @Transactional
    @Override
    public int addArticle(ArticleVo article) {
        return editMapper.insertArticle(article);
    }

    /**
     * 客户端动态查询文章
     *
     * @param requestVo
     * @return
     */
    @Override
    public PageVo<ArticleVo> articleSelectQuery(ArticlePageRequestVo requestVo) {
        try {
            List<ArticleVo> articleList = editMapper.dynamicQueryArticle(requestVo);
//            log.info("客户端动态查询文章"+articleList);
            long sum = editMapper.dynamicQueryArticleCount(requestVo);
            for (ArticleVo ar : articleList) {
                if (ar.getImg() != null && !ar.getImg().equals(" ")) {
                    //将json字符串转为数组
                    JSONArray objects = JSONUtil.parseArray(ar.getImg());
                    List<String> list = JSONUtil.toList(objects, String.class);
                    if (list.size() > 2) {
                        ar.setCoverImg(CollectionUtil.sub(list, 0, 3));
                    } else {
                        ar.setCoverImg(CollectionUtil.sub(list, 0, 1));
                    }
                    ar.setImg(null);
                }
            }

            PageVo<ArticleVo> articlePage = new PageVo<>();
            articlePage.setPageList(articleList);
            articlePage.setTotal(sum);
            return articlePage;
        } catch (Exception e) {
            log.info(e + "");
            throw new BaseException(ExceptionEnum.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 指定用户id分页查询文章
     *
     * @param requestVo
     * @return
     */
    @Override
    public PageVo<ArticleVo> articleSelectQueryByUserId(ArticlePageRequestVo requestVo) {
        List<ArticleVo> articleList = editMapper.queryArticleByUserId(requestVo);
        if (articleList.isEmpty()) {
            throw new BaseException(ExceptionEnum.NOT_FOUND_ARTICLE);
        }
        for (ArticleVo ar : articleList) {
            if (ar.getImg() != null && !ar.getImg().equals(" ")) {
                //将json字符串转为数组
                JSONArray objects = JSONUtil.parseArray(ar.getImg());
                List<String> list = JSONUtil.toList(objects, String.class);
                if (list.size() > 1) {
                    ar.setCoverImg(CollectionUtil.sub(list, 0, 1));
                }
                ar.setImg(null);
            }
        }
        long count = editMapper.articlesCount(requestVo.getCreateUserId());
        PageVo<ArticleVo> articlePage = new PageVo<>();
        articlePage.setPageList(articleList);
        articlePage.setTotal(count);
        return articlePage;
    }

    /**
     * 删除文章
     *
     * @param articleId
     * @param userId
     * @return
     */
    @SysLog("用户删除了文章")
    @Transactional
    @Override
    public int deleteArticle(int articleId, int userId) {
        return editMapper.deleteArticle(articleId, userId);
    }

    //分页搜搜用户
    @SysLog("搜索用户")
    @Override
    public PageVo<ArticleVo> searchUser(Integer page, Integer size, String userName, Integer userId) {

      List<ArticleVo> queryUserPage =  editMapper.queryUserPage(page,size,userName,userId);
        long sum = editMapper.countQueryUser(userName);
        PageVo<ArticleVo> pageVo = new PageVo<>();
        pageVo.setPageList(queryUserPage);
        pageVo.setTotal(sum);
        return pageVo;
    }


}
